{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "ICS 104 - Introduction to Programming in Python and C  \n",
    "# Programming with numbers and Strings\n",
    "## Reading Assignment\n",
    "- Chapter 2 Sections 1, 2, 4 and 5."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Chapter Learning Outcomes\n",
    "### At the end of this chapter, you will be able to\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- define and use variables and constants\n",
    "- write arithmetic expressions and assignment statements\n",
    "- understand the properties and limitations of integers and floating-point numbers\n",
    "- appreciate the importance of comments and good code layout\n",
    "- write arithmetic expressions and assignment statements\n",
    "- create programs that read and process inputs, and display the results\n",
    "- learn how to use Python strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Variables\n",
    "## Why do we need variables?\n",
    "- To carry out computation, we need to store values in order to use them later on.\n",
    "- These values are stored in variables.\n",
    "- Let us try to comprehend the use of variables by solving the following problem:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Soft Drinks: Which is more Economic?\n",
    "- Soft drinks are sold in cans and bottles. \n",
    "- A store offers a six-pack of 12-ounce cans for the same price as a two-liter bottle. \n",
    "- Find the volume (in liters) of a six-pack of soda cans and the total volume of a six-pack and a two-liter bottle.\n",
    "    - Note that 12 fluid ounces equal approximately 0.355 liters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Defining Variables\n",
    "- A variable is a storage location in a computer program. \n",
    "- Each variable has a name and holds a value."
   ]
  },
  {
   "attachments": {
    "ch02Fig01.png": {
     "image/png": "[encoded data, 699104 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch02Fig01.png](attachment:ch02Fig01.png)\n",
    "- Just as a parking space has an identifier **J053** and contents **car**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Assignment Statements\n",
    "- An **assignment** statement is used to place a value into a variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "cansPerPack = 6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- How does the assignment statment work?\n",
    "    - The right hand side of the **=** sign is first evaluated (to the value 6).\n",
    "    - The value is assigned to the variable on the left hand side of the **=** sign (to the variable **cansPerPack**)."
   ]
  },
  {
   "attachments": {
    "df5b3a54-649b-44d6-9acc-ca8c639539b0.png": {
     "image/png": "[encoded data, 33320 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch02Fig2.png](attachment:df5b3a54-649b-44d6-9acc-ca8c639539b0.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- Once a variable is defined, it can be used in other statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(cansPerPack)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- If an existing variable is assigned a **new** value, that value replaces the previous contents of the variable. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "cansPerPack = 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(cansPerPack)"
   ]
  },
  {
   "attachments": {
    "6c30e52d-db0b-45ce-bf98-6958493d6864.png": {
     "image/png": "[encoded data, 19940 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch02Fig03.png](attachment:6c30e52d-db0b-45ce-bf98-6958493d6864.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Assignment is not Equality in Algebra\n",
    "- <font color='red'>Is the statement</font>\n",
    "                cansPerPack = cansPerPack + 2\n",
    "  <font color='red'>correct in Algebra?</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- <font color='red'>How about in Python?</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "cansPerPack = 8\n",
    "cansPerPack = cansPerPack + 2\n",
    "print(cansPerPack)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- So, how does the assignment <font color='blue'> cansPerPack = cansPerPack + 2</font> execute in python? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- First, the right hand side is executed\n",
    "    - This is done by fetching the current value of the variable **cansPerPack**\n",
    "    - Then, carrying out the addition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Second, the value of the addition is stored in the variable **cansPerPack**"
   ]
  },
  {
   "attachments": {
    "07a6d7ac-4abe-488f-9527-48bde1efd947.png": {
     "image/png": "[encoded data, 43308 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> ![ch02Fig04.png](attachment:07a6d7ac-4abe-488f-9527-48bde1efd947.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Number Types\n",
    "## Values and Types\n",
    "- <font color='blue'>**2**</font>, <font color='green'>**\"Hello World\"**</font> and <font color='purple'>**8.4**</font> are **values**\n",
    "- Each value belongs to a <font color='red'>**data type**</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    ">    - <font color='blue'>**2**</font> is an **integer** <font color='red'>**int**</font>\n",
    ">    - <font color='green'>**\"Hello World\"**</font> is a **string** <font color='red'>**str**</font>\n",
    ">    - <font color='purple'>**8.4**</font> is a **float** <font color='red'>**float**</font>\n",
    ">    - <font color='blue'>**2**</font> and <font color='purple'>**8.4**</font> are called **number literals**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Why Data Types?\n",
    "- A **data type** of a value determines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "> - how the data type is represented in the computer, and\n",
    "> - what operations can be performed on that data."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Two Categories of Data Types in Python\n",
    "- <font color='blue'>Primitive data type</font>\n",
    "    - A data type provided by the language itself (e.g. <font color='red'>**int**</font>)\n",
    "- <font color='blue'>User-defined data type</font>\n",
    "    - A data type defined by the programmer (covered in Chapter 9: **Objects and Classes**)"
   ]
  },
  {
   "attachments": {
    "ch02Fig05.png": {
     "image/png": "[encoded data, 146064 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Number literals in Python\n",
    "> ![ch02Fig05.png](attachment:ch02Fig05.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The value determines the type of the variable.\n",
    "- For example, the following piece of code is correct, but not recommended"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "taxRate = 5\n",
    "print(taxRate)\n",
    "taxRate = 5.5\n",
    "print(taxRate)\n",
    "taxRate = \"five point five\"\n",
    "print(taxRate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- This is not a good idea, as it may lead to an error if you use the wrong operation on the variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "taxRate = taxRate + 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Once a variable is initialized with a value of a type, keep storing values of the same type."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Rules for Variable Names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Names must start with a letter or the underscore (\\_) character.\n",
    "- The remaining characters (if any) must be letters, digits or underscores. \n",
    "    - Symbols such as ? or % cannot be used in a variable name.\n",
    "    - Spaces cannot exist within a variable name.\n",
    "- Names are case sensitive.\n",
    "- Reserved words by python cannot be used as variable names. (e.g., <font color='blue'> **if**</font> and <font color='blue'>**class** </font>)  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- Which of the following names are proper variable names?\n",
    "    `canVolume1 , x , CanVolume , 6pack , can volume , class , ltr/fl.oz`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `canVolume1` is proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `x` is proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `CanVolume` is proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `6pack` is not proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `can volume` is not proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `class` is not proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- `ltr/fl.oz` is not proper"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Recommended Variable Name Conventions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- These are not strict rules for variable names, but are **rules of good taste** that you should respect when writing code.\n",
    "    - Use a descriptive name, such as **`cansPerPack`**, than a terse name, such as **`cpp`**\n",
    "        - If the variable name consists of more than one word, start the word with a capital letter, as shown above.\n",
    "    - A variable starts with a small letter\n",
    "    - A constant consists of all capital letters, where words are separated by the underscore **`_`** character, such as **`CAN_VOLUME`**\n",
    "    - A user defined data type starts with a capital letter (as we will see later), such as **`GraphicsWindow`**.  "
   ]
  },
  {
   "attachments": {
    "ch02Fig06.png": {
     "image/png": "[encoded data, 173948 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Therefore,\n",
    "![ch02Fig06.png](attachment:ch02Fig06.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Constants"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- A constant variable, or simply a **constant**, is a variable whose value should not be changed after it has been assigned an initial value.\n",
    "- Some languages provide an explicit mechanism of declaring constants.\n",
    "    - Hence, any attempt to change it after it has been assigned generates a syntax error.\n",
    "- Python leaves it to the programmer to make sure that constants are not changed.\n",
    "    - Hence, the use of all capital letters for naming constants tells you and other programmers that you should not change the value of this **variable** once it is assigned.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- Constants can make your code much more understandable."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- For example, compare the following two statements:\n",
    "    - `totalVolume = bottles * 2`\n",
    "    - `totalVolume = bottles * BOTTLE_VOLUME`\n",
    "- Note that in the case where the bottle volume is changed from 2 to 2.5, then\n",
    "    - in the first case, you need to change every line of code that has volume 2 to 2.5.\n",
    "    - in the second case, all you need to do is change the value of the constant **BOTTLE_VOLUME** to 2.5 in one line ONLY. Every other occurrence of **BOTTLE_VOLUME** in the code will `automatically` have the new volume value."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Comments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- As your programs get more complex, you should add **comments**, _explanations for human readers of your code_."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "CAN_VOLUME = 0.355   # Liters in a 12-ounce can"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- This comment explains the significance of the value 0.355 to a human reader. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Python's interpreter does not execute comments at all.\n",
    "    - It ignores everything from a **#** delimiter to the end of the line."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Why Write Comments?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Helps programmers who read your code understand your intent."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Helps you when you review your code (after some time). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# How to Write Comments?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Provide a comment at the top of your source file that explains the purpose of the program. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The textbook follows the following style:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "##\n",
    "# This program computes the volume (in liters) of a six-pack of soda cans.\n",
    "#"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Time to Solve the Problem at the Beginning of this Chapter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Soft Drinks: Which is more Economic?\n",
    "- Soft drinks are sold in cans and bottles. \n",
    "- A store offers a six-pack of 12-ounce cans for the same price as a two-liter bottle. \n",
    "- Which one should you buy?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Solution Steps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Compute the **totalVolume** you get when you buy a six-pack\n",
    "    - Define **CAN_VOLUME** and the number of **cansPerPack**\n",
    "    - `totalVolume = cansPerPack * CAN_VOLUME`\n",
    "    - print the **totalVolume**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Now you can compare the **totalVolume** to the value **2.0** and determine which one to buy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "##\n",
    "#  This program computes the volume (in liters) of a six-pack of soda\n",
    "#  cans and the total volume of a six-pack and a two-liter bottle.\n",
    "#\n",
    "\n",
    "# Liters in a 12-ounce can and a two-liter bottle.\n",
    "CAN_VOLUME = 0.355\n",
    "BOTTLE_VOLUME = 2\n",
    "\n",
    "# Number of cans per pack.\n",
    "cansPerPack = 6\n",
    "\n",
    "# Calculate total volume in the cans.\n",
    "totalVolume = cansPerPack * CAN_VOLUME\n",
    "print(\"A six-pack of 12-ounce cans contains\", totalVolume, \"liters.\")\n",
    "\n",
    "# Calculate total volume in the cans and a two-liter bottle.\n",
    "totalVolume = totalVolume + BOTTLE_VOLUME\n",
    "print(\"A six-pack and a two-liter bottle contain\", totalVolume, \"liters.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Final Tips on Variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Do not use undefined variables\n",
    "    - `canVolume = 12 * literPerOunce # Error`<br>\n",
    "    - `literPerOunce = 0.0296`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Choose descriptive variable names\n",
    "    - **canVolume** is better than **cv**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Do not use *magic* numbers\n",
    "    - `totalVolume = cansPerPack * 0.355`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 2.2 Arithmetic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "## Basic Arithmetic Operations\n",
    "- Python supports addition `+`, subtraction `-`, multiplication `*` and division `/` \n",
    "- **`+ - * /`** are called **operators**\n",
    "- The combination of variables, literals, operators, and parentheses is called an arithmetic **expression**\n",
    "- For example, the mathematical formula $\\frac{a+b}{2}$ is written in python as `(a + b) / 2`\n",
    "    - Note that the parentheses are used to determine in which order the parts of the expression are computed.\n",
    "    - For example, which mathematical formula is `a + b / 2`?\n",
    "- Python uses the exponential operator $**$ to denote the power operation.\n",
    "    - For example, $a^2$ is `a ** 2`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Precedence of Arithmetic Operators\n",
    "- Python uses the precedence rules for algebraic notation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "| Precedence | Operator(s)| Description |\n",
    "| :---: | :---: | :---|\n",
    "|1 | $( )$ | Parentheses|\n",
    "|2 | $**$ | Power|\n",
    "|3 | $* , /$ | Multiplication and Division|\n",
    "|4 | $+ , -$ | Addition and Subtraction|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Order of Evaluation of Arithmetic Operators\n",
    "- Addition, subtraction, multiplication and division are left associative, i.e. they are evaluated from left to right.\n",
    "    - For example, `10 + 2 + 3` is evaluated as $(10+2)+3 = 15$ \n",
    "- The power operation is right associative, i.e. it is evaluated from right to left.\n",
    "    - For example, `10 ** 2 ** 3` is evaluated as $10^{2^3}$ which is the same as $10^{8} = 100000000$"
   ]
  },
  {
   "attachments": {
    "ch02Fig07.png": {
     "image/png": "[encoded data, 27308 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Example\n",
    "- The mathematical expression $b\\times (1+\\frac{r}{100})^n$ becomes <br>\n",
    "> `b * (1 + r / 100) ** n` <br>\n",
    "- The expression is analyzed as follows\n",
    "> ![ch02Fig07.png](attachment:ch02Fig07.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Floor Division and Remainder\n",
    "- Division of two integers results in a floating-point value\n",
    "    - `7 / 4` yields `1.75`\n",
    "- The floor division operator `//` when applied on positive integers computes the quotient and discards the fractional part.\n",
    "    - `7 // 4` yields 1\n",
    "- The **modulus** operator `%` can be used to get the remainder of the floor division.\n",
    "    - `7 % 4` yields 3, the remainder of the floor division of 7 by 4.\n",
    "    - Some also call it **modulo** or **mod**"
   ]
  },
  {
   "attachments": {
    "ch02Fig08.png": {
     "image/png": "[encoded data, 100788 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Floor Division and Remainder\n",
    "![ch02Fig08.png](attachment:ch02Fig08.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Calling Functions\n",
    "- We have been using the print function to display information, but there are many other functions available in Python.\n",
    "- Most functions **return** a value. \n",
    "    - i.e., when the function completes its task, it passes a value back to the point where the function was called.\n",
    "    - For example, the call `abs(-123)` returns the value 123.\n",
    "- The value returned by a function can be stored in a variable.\n",
    "    - `distance = abs(x)`\n",
    "    - Note that `x` is called the **argument** of the **`abs`** function. \n",
    "- It can also be used anywhere that a value of the same type can be used\n",
    "    - `print(\"The distance from the origin is \", abs(x))`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Arguments of a Function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- When calling a function, you must provide the correct number of arguments.\n",
    "    - `abs(-10, 2)` or `abs()` will generate an error.\n",
    "    - Hence, the **`abs`** function requires exactly one argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "abs(-10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Some functions have optional arguments that you only provide in certain situations\n",
    "    - For example, in the **`round`** function\n",
    "        - `round(7.624)` returns the nearest integer, i.e. `8`\n",
    "        - `round(7.624,2)` returns the nearest floating-point with 2 decimal digits, i.e. `7.62`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "print(round(7.624))\n",
    "print(round(7.624,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Some functions take an arbitrary number of arguments\n",
    "    - For example, the **`max`** and **`min`** functions.\n",
    "        - `min(7.25, 10.95, 5.95, 6.05, 8)` returns the minimum of the function's arguments; in this case the number `5.95`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [],
   "source": [
    "min(7.25, 10.95, 5.95, 6.05, 8)"
   ]
  },
  {
   "attachments": {
    "ch02Fig09.png": {
     "image/png": "[encoded data, 103056 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Calling Functions\n",
    "> ![ch02Fig09.png](attachment:ch02Fig09.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Libraries\n",
    "- A **library** is a collection of code that has been written and translated by someone else, ready for you to use in your program.\n",
    "    - A **standard library** is a library that is considered part of the language and must be included with any Python system."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Python’s standard library is organized into modules.\n",
    "    - Related functions and data types are grouped into the same module."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Mathematical Functions\n",
    "- Python’s **math** module includes a number of mathematical functions.\n",
    "- You must **import** it before you can use any of its functions\n",
    "    - Note that you can use the **`print`** function without the use of **import**, since it is one of the **built-in** functions (part of the Python language and can be used directly in your programs)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "y = sqrt(25)\n",
    "print(\"y = \", y)"
   ]
  },
  {
   "attachments": {
    "ch02Fig10.png": {
     "image/png": "[encoded data, 130440 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch02Fig10.png](attachment:ch02Fig10.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- To import more than one function from **math**, use **`from math import *`** "
   ]
  },
  {
   "attachments": {
    "ch02Fig11.png": {
     "image/png": "[encoded data, 117932 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Arithmetic Expressions Examples\n",
    "> ![ch02Fig11.png](attachment:ch02Fig11.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Student Activity\n",
    "- The volume of a sphere is given by $$V=\\frac{4}{3}\\pi r^3$$\n",
    "If the radius is given by a variable **radius** that contains a floating-point value,\n",
    "write a Python expression for the volume."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# Volume Expression\n",
    "radius = 2.4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 2.4 Strings\n",
    "- A **string** is a sequence of characters\n",
    "    - **Characters** include letters, numbers/digits, punctuation, spaces, special symbols and so on.\n",
    "- A **string literal** denotes a particular string (e.g. \"Hello\")\n",
    "    - Just as a number literal (e.g. `34`) denotes a particular number.\n",
    "    - String literals are specified by enclosing a sequence of characters within a matching pair of either single or double quotes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"This is a string. \", 'So is this.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- How can I form the strings **`I'm a student`** or **`He said: \"You did it!\"`**?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"I'm a student\", 'He said: \"You did it!\"')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The number of characters in a string is called the **length** of the string. \n",
    "    - For example, \"Harry\" is of length \\_\\_\\_\\_\\_ and \"World\" is of length \\_\\_\\_\\_\\_\\_\n",
    "    - An **empty** string is a string with no characters. It is of length zero and is written as `\"\"` or `''`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Python's **`len`** function returns the length of the argument string.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "length = len(\"World!\")\n",
    "print(length)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# String Concatenation\n",
    "- Given two strings such as **`Ahmad`** and **`Saleem`**, you can **concatenate** them to one long string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "firstName = \"Ahmad\"\n",
    "secondName = \"Saleem\"\n",
    "name = firstName + secondName\n",
    "print (name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Note that if one of the operands of the **`+`** operator is a string, then all of them should be strings, otherwise a syntax error will occur."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "print(\"The character with value 1710 is the \", chr(1710))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# String Repetition\n",
    "- Given a string such as **`-`**, you can repeat it **n** times, where **n** is an integer using the string repetition operator **`*`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "dashes = \"-\" * 50\n",
    "print(dashes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Converting between Numbers and Strings\n",
    "- Since you cannot concatenate a string and integer, Python provides the **`str`** function to convert an integer to a string. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "id = 2019873410\n",
    "email = \"s\" + str(id) + \"@kfupm.edu.sa\"\n",
    "print(email)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Conversely, you can turn a string representing a number into its corresponding numerical value using the **`int`** and **`float`** functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "id = int(\"1729\")\n",
    "price = float(\"17.29\")\n",
    "print(\"id is\", id, \" and price is\", price)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Strings and Characters\n",
    "- Strings are sequences of **Unicode** characters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Individual characters of a string can be accessed based on their position in the string\n",
    "    - The position is called the **index** of the character.\n",
    "    - The **index** starts from position 0, followed by 1 for the second character, ... and so on."
   ]
  },
  {
   "attachments": {
    "ch02Fig12.png": {
     "image/png": "[encoded data, 4608 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- **`name = \"Harry\"`**\n",
    "![ch02Fig12.png](attachment:ch02Fig12.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "name = \"Harry\"\n",
    "first = name[0]\n",
    "last = name[4]\n",
    "print(first,last)"
   ]
  },
  {
   "attachments": {
    "ch02Fig13.png": {
     "image/png": "[encoded data, 10284 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch02Fig13.png](attachment:ch02Fig13.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The index value must be within the valid range of character positions\n",
    "    - 0 .. `len(name)-1`\n",
    "- otherwise, an \"index out of range\" exception will be generated at run time."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Student Activity\n",
    "- What are the results of the following statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "string = \"Py\"\n",
    "string = string + \"thon\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(string)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "print (\"Please\" +\n",
    "\" enter your name: \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"Please\" +\n",
    "      \" enter your name: \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- What is the result of the following statements"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "team = str(49) + \"ers\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"team = \", team)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "greeting = \"H & S\"\n",
    "n = len(greeting)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"n = \", n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "string = \"Harry\"\n",
    "n = len(string)\n",
    "mystery = string[0] + string[n - 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(mystery)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 2.5 Input and Output\n",
    "- Asking the user to provide input values makes programs more flexible.\n",
    "    - As opposed to having fixed values."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- For example, You will have to change the values of `first` and `second` in the program below every time you would like to use different values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "##\n",
    "#  This program prints a pair of initials.\n",
    "#\n",
    "\n",
    "# Set the names of the couple.\n",
    "first = \"Rodolfo\"\n",
    "second = \"Sally\"\n",
    "\n",
    "# Compute and display the initials.\n",
    "initials = first[0] + \"&\" + second[0]\n",
    "print(initials)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- When a program asks for user input, it should first print a message (called a **prompt**) that tells the user which input is expected.\n",
    "- In Python, displaying a prompt and reading the keyboard input is combined in one operation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "##\n",
    "#  This program obtains two names from the user and prints a pair of initials.\n",
    "#\n",
    "\n",
    "# Obtain the two names from the user.\n",
    "first = input(\"Enter your first name: \")\n",
    "second = input(\"Enter your significant other's first name: \")\n",
    "\n",
    "# Compute and display the initials.\n",
    "initials = first[0] + \"&\" + second[0]\n",
    "print(initials)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Note that the output of the **`input`** function is always a **string**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Reading Numerical Input\n",
    "- What if we need to read a numerical input?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Use the string conversion functions **int** and **float** on the output string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "userInput = input(\"Please enter the number of bottles: \")\n",
    "numberOfBottles = int(userInput)\n",
    "bottleVolume = float(input(\"Enter the volume of each bottle: \")) # preferred style\n",
    "print(\"The number of bottles = \", numberOfBottles, \" and the bottle volume = \", bottleVolume)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Formatted Output\n",
    "## Formatting Floating Point Values\n",
    "- When you print the result of a computation, you often want to control its appearance. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "| Instead of | | Would Like to Print|\n",
    "|:---|:---| :---|\n",
    "|  Price per liter: 1.215962441314554 | | Price per liter: 1.22 |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- We can do that through the **string format operator %**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The following command displays the price with two digits after the decimal point:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "price = 1.215962441314554\n",
    "print(\"%.2f\" % price)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- You can also specify a field width (the total number of characters, including spaces)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "price = 1.215962441314554\n",
    "print(\"%10.2f\" % price)"
   ]
  },
  {
   "attachments": {
    "ch02Fig14.png": {
     "image/png": "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"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "![ch02Fig14.png](attachment:ch02Fig14.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- **`%10.2f`** is called a **format specifier**.\n",
    "- See what happens when you play with the values of the **format specifier**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Formatting Integer and String Values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Use **%d** for integer values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "numberOfBottles = 106\n",
    "print(\"%d\" % numberOfBottles)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Use **%s** for string values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "title2=\"Price:\"\n",
    "print(\"%-10s\" % title2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Multiple Format Specifiers\n",
    "- One can have more than one format specifier in the **format string**\n",
    "- In this case, the variables to the right of the string format **operator %** need to be included between parentheses and separated by commas. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "quantity = 203\n",
    "price = 183.4\n",
    "title1 = \"Quantity:\"\n",
    "title2 = \"Price:\"\n",
    "print(\"%10s %10d\" % (title1, quantity))\n",
    "print(\"%10s %10.2f\" % (title2, price))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- You can play with different values and see what happens to the output\n",
    "    - print(\"%-10s %10d\" % (title1, quantity))\n",
    "    - print(\"%-10s %10.2f\" % (title2, price))     # Strings are left aligned, numbers are right aligned\n",
    "    - \n",
    "    - print(\"%10s %-10d\" % (title1, quantity))    # Strings are right aligned, numbers are left aligned\n",
    "    - print(\"%10s %-10.2f\" % (title2, price))\n",
    "    - \n",
    "    - print(\"%-10s %-10d\" % (title1, quantity))   # Strings and numbers are left aligned\n",
    "    - print(\"%-10s %-10.2f\" % (title2, price))\n"
   ]
  },
  {
   "attachments": {
    "ch02Fig15.png": {
     "image/png": "[encoded data, 131528 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## String Format Operator\n",
    "![ch02Fig15.png](attachment:ch02Fig15.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "- The following statement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "quantity = 24\n",
    "total = 17.29\n",
    "print(\"Quantity: %d Total: %10.2f\" % (quantity, total))"
   ]
  },
  {
   "attachments": {
    "ch02Fig16.png": {
     "image/png": "[encoded data, 65348 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- produces\n",
    "![ch02Fig16.png](attachment:ch02Fig16.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Student Activity\n",
    "- What is problematic about the following statement sequence?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "userInput = input(\"Please enter the number of cans\")\n",
    "cans = int(userInput)"
   ]
  },
  {
   "attachments": {
    "ch02Fig17.png": {
     "image/png": "[encoded data, 65888 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Student Activity\n",
    "![ch02Fig17.png](attachment:ch02Fig17.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "# To Print Bottles and Cans \n",
    "bottles = 8\n",
    "cans = 24\n",
    "## Insert your solution here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "# Different solutions:\n",
    "print(\"Bottles: %8d\" % bottles)\n",
    "print(\"Cans:    %8d\" % cans)\n",
    "    \n",
    "print(\"Bottles: %8d\" % bottles)\n",
    "print(\"Cans: %11d\" % cans)\n",
    "    \n",
    "print(\"%-8s %8d\" % (\"Bottles:\", bottles))\n",
    "print(\"%-8s %8d\" % (\"Cans:\", cans))"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
